package com.lagou.dao;

import com.lagou.entity.Account;
import com.lagou.entity.Transaction;

import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class AccountDao {
    /**
     * 实现转账操作
     * accountCard1 为转出方的卡号
     * accountCard2 为转入放的卡号
     * @return
     */
    public boolean transferAccounts(String accountCard1, String accountCard2, double money) throws SQLException {
        //判断转出方是否有足够余额，如果不足，提示信息：”余额不足！”，并结束程序
        if (queryAccountBalance(accountCard1) < money) {
            System.out.println("余额不足！请及时充值");
            return false;
        }
        //获取QueryRunner 手动模式
        QueryRunner qr = new QueryRunner();
        Connection con = null;
        try {

            //获取数据库
            con= DruidUtils.getConnection();
            //开启事务
            con.setAutoCommit(false);
            //编写sql
            String sql = "update account set balance = balance + ? where card = ?";
            Object[] param = {0 - money, accountCard1};
            qr.update(con, sql, param);
            Transaction transaction = new Transaction(accountCard1,"转出",money, DateUtils.getDateFormart());

            Object[] param1 = {money, accountCard2};
            qr.update(con, sql, param1);
            Transaction transaction1 = new Transaction(accountCard2,"转入",money,DateUtils.getDateFormart());
            // 转账结束后, 将转入、转出记录分别写入到Transaction表中。
            TransactionDao transactionDao = new TransactionDao();
            if (transactionDao.transactionRecord(transaction) > 0) {
                System.out.println("转出记录已写入");
            }
            if (transactionDao.transactionRecord(transaction1) > 0) {
                System.out.println("转入记录已写入");
            }
            //使用dnutils提交事务 并关闭
            DbUtils.commitAndCloseQuietly(con);
        } catch (Exception e) {
            //异常回滚
            DbUtils.rollbackAndCloseQuietly(con);
            e.printStackTrace();
            return false;
        } finally {
            //释放资源
            DbUtils.closeQuietly(con);
        }
        return true;

    }
    /**
     * 查询账户余额
     * accountcard 账户卡号
     */
    public double queryAccountBalance(String accountCard) throws SQLException {

        //获取QueryRunner 自动模式
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //编写SQL
        String sql = "SELECT balance FROM account WHERE card = ?";

        return qr.query(sql, new ScalarHandler<Double>(), accountCard);
    }
    /**
     * 查询账户所有的信息
     */
    public Account getAccountFromCard(String accountCard) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from account where card = ?";

        return  qr.query(sql, new BeanHandler<Account>(Account.class), accountCard);
    }



}
